<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>修改</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
     <script src="./js/jquery-1.11.1.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/main.js"></script>
    <style type="text/css">
        body {
            background: #2fb9e2;
            min-width: 1000px;
        }
        
        .content {
            margin: auto;
            margin-top: 100px;
            padding: 7%;
            padding-bottom: 100px;
            margin-bottom: 100px;
            width: 800px;
            height: auto;
            background-color: #fff;
            border-radius: 20px;
            box-shadow: 0px 0px 20px #5b7884;
        }
        
        .table {
            text-align: center;
            margin: auto;
        }
        
        #add {
            float: right;
        }
        
        #title {
            margin: auto;
            padding-top: 5px;
            padding-bottom: 5px;
        }
        
        .center {
            text-align: center;
            margin: auto;
        }
        
        #logo {
            margin-bottom: 50px;
        }
        
        img {
            width: 150px;
        }

        .btn {
            margin-right: 10px;
        }
    </style>
</head>
<script>
    if (!getCookie('sessionid')){
            alert("没有登录")
            window.location.href = "login.html"
        }
    if(typeof FileReader == 'undefined') {
        result.innerHTML = "抱歉，你的浏览器不支持图片预览,请使用最新版的Chrome，FireFox等浏览器";
    }

$(document).ready(function() {
    var pid = QueryString.id;
    getProviderInfo(pid)
    if (!pid){
        alert("没有id");
        window.location.href="manage.html"
    }
    $("#confirm_modify").click(function(){
        //alert("修改");
        var flag = checkData();
        if (flag){
            uploadProviderInfo(pid);
        }else{
            $("#modal_confirm").modal('hide');
        }
       
    })
})
    

    function back() {
        window.location.href = "manage.html"
    }
    function show() {
        $("#modal_confirm").modal('show');
    }
    function checkData(){
        var s = getInputData();
        var flag = true;
    //alert(s)
        $.each(s,function(name,value){
            //alert(name + "," + value);
            if (name=="delivermin" || name == "delivercost"){
                if(!checkInputNumber(name,value)){
                    flag = false;
                }
            }else{
                if(!checkInput(name,value)){
                    flag = false;
                }
            }
            
        })
        return flag;
    }
    function checkImgChosed(simpleFile){
        if (simpleFile == undefined) {
            alert("您还没有选择头像")
            return;
        };
    }
    function getProviderInfo(id){
        var session = getCookie('sessionid');
    //alert(session + "," + id);
        //访问网络
    var dataSend = new Object();
    dataSend.pid = id;
    dataSend.module = "provider";
    dataSend.action = "getProviderDetails";
    dataSend.sessionid = session;
        var settings = {
                type: "POST",
                url: "../../Fanxiangle/servlet/manager",
                dataType: "json",
                data: dataSend,
                error: function(XHR, textStatus, errorThrown) {
                    alert("网络错误");
                },
                success: function(data, textStatus) {
                    if (textStatus == "success") {
                        if (data.status == true) {
                            //alert("data:" + data.data);
                            $("#name").val(data.data.name);
                            $("#address").val(data.data.address);
                            $("#delivermin").val(data.data.delivermin);
                            $("#delivercost").val(data.data.delivercost);
                            $("#status").val(data.data.status);
                            $("#notice").val(data.data.notice);
                            $("#description").val(data.data.description);
                            $("#img").attr('src','../images/providers/avatars/'+id+'.png')
                        } else if (data.errorCode == 1001) {
                            alert("参数不全")
                        } else if (data.errorCode == 1002) {
                            alert("尚未登录")
                            window.location.href = "login.html"
                        } else if (data.errorCode == 1006) {
                            alert("无此接口")
                        } else if (data.errorCode == 1010) {
                            alert("数据库错误")
                        } else if (data.errorCode == 1000) {
                            alert("未知错误")
                        }
                        //alert("data:" + data.data + ",status:" + textStatus)
                    }else{
                        alert("服务器错误");
                    }

                }
            };
            $.ajax(settings);
    }

    function uploadProviderInfo(id){
        var session = getCookie('sessionid');
        //alert(session + "," + id);
        var dataSend = new Object();
        dataSend = getInputData();
        dataSend.pid = id;
        dataSend.module = "provider";
        dataSend.action = "modifyProviderInfo";
        dataSend.sessionid = session;
        var data = new FormData();
        $.each(dataSend,function(name,value){
            data.append(name,value);
        });
        var simpleFile = document.getElementById("file").files[0];
        if (simpleFile == undefined) {
            //alert("没有");
        }else {
            data.append('avatar', simpleFile);
        }
               
          
        //访问网络
        var settings = {
                type: "POST",
                url: "../../Fanxiangle/servlet/manager",
                //dataType: "json",
                processData: false,
                contentType: false,
                cache: false,
                data: data,
                error: function(XHR, textStatus, errorThrown) {
                    alert("网络错误");
                },
                success: function(row, textStatus) {
                    var data = JSON.parse(row);
                    //alert(data.status + "," + textStatus);
                    if (textStatus == "success") {
                        if (data.status == true) {
                            alert("修改成功");
                            $("#modal_confirm").modal('hide');
                            //window.location.href = "manage.html"
                        } else if (data.errorCode == 1001) {
                            alert("参数不全")
                        } else if (data.errorCode == 1002) {
                            alert("尚未登录")
                            window.location.href = "login.html"
                        } else if (data.errorCode == 1006) {
                            alert("无此接口")
                        } else if (data.errorCode == 1010) {
                            alert("数据库错误")
                        } else if (data.errorCode == 1000) {
                            alert("未知错误")
                            $("#modal_confirm").modal('hide');
                        }
                        //alert("data:" + data.data + ",status:" + textStatus)
                    }else{
                        alert("服务器错误");
                    }

                }
            };
            $.ajax(settings);
    }
    function getInputData(){
        var info = new Object();
        info.name = $("#name").val();
        info.delivermin = $("#delivermin").val();
        info.delivercos = $("#delivercost").val();
        info.notice = $("#notice").val();
        info.description = $("#description").val();
        info.address = $("#address").val();
        info.status = $("#select").val();
        return info;
    }
    // 将文件以Data URL形式进行读入页面
    function readAsDataURL(){
        //alert("in");
        // 检查是否为图像类型
        var simpleFile = document.getElementById("file").files[0];
        if (simpleFile == undefined) {
            alert("您还没有选择头像")
            return;
        };
        if(!/image\/\w+/.test(simpleFile.type)) {
            alert("请确保文件类型为图像类型");
            return false;
        }
        var reader = new FileReader();
        // 将文件以Data URL形式进行读入页面
        reader.readAsDataURL(simpleFile);
        reader.onload = function(e){
            $("#img").attr("src",this.result)
            console.log(this.result);
        }
    }
</script>
<body>
    <div class="modal fade" id="modal_confirm">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <!-- <span aria-hidden="true">&times;</span>
                -->
            </button>
            <h4 class="modal-title">饭享乐-确认修改</h4>
        </div>
        <div class="modal-body">
            <p>
                是否确认修改信息?
            </p>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            <button type="button" class="btn btn-primary" id="confirm_modify">确认</button>
        </div>
    </div>
    <!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
    </div>
    <div class="container">
        <div class="content">
            <h1 class="center" id="logo">饭享乐-修改大妈信息</h1>
            <form class="form-horizontal">
                <div class="form-group">
                    <label for="name" class="col-sm-2 control-label">名称</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="name" placeholder="可选">
                    </div>
                </div>
                <div class="form-group">
                    <label for="delivermin" class="col-sm-2 control-label">起送价格</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="delivermin" placeholder="可选">
                    </div>
                </div>
                <div class="form-group">
                    <label for="delivercost" class="col-sm-2 control-label">配送费用</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="delivercost" placeholder="可选">
                    </div>
                </div>
                 <div class="form-group">
                    <label for="notice" class="col-sm-2 control-label">通知</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="notice" placeholder="可选">
                    </div>
                </div>
                <div class="form-group">
                    <label for="description" class="col-sm-2 control-label">简介</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="3" id="description" placeholder="可选"></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label for="address" class="col-sm-2 control-label">地址</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="address" placeholder="可选">
                    </div>
                </div>
                <div class="form-group">
                    <label for="status" class="col-sm-2 control-label">状态</label>
                    <div class="col-sm-10">
                        <select class="form-control" id="select">
                            <option value="0">休息中</option>
                            <option value="1">接受预定</option>
                            <option value="2">开店中</option>
                        </select>
                    </div>
                </div>
                
                <div class="form-group">
                    <label for="avatar" class="col-sm-2 control-label">上传头像</label>
                    <input type="file" id="file" name="avatar" onchange="readAsDataURL()">
                    <p class="help-block col-sm-3 control-label">头像不要大过500*500</p>
                </div>
                
                <div class="form-group">
                    <label for="img" class="col-sm-2 control-label">头像</label>
                    <div class="col-sm-10">
                        <img src="./logo.png" alt="" id="img" class="image-rounded">
                    </div>
                </div>
                
                    
            </form>
            <div class="col-sm-offset-2 col-sm-10">
                 <button class="btn btn-primary" onclick="show()">确认修改</button>
                 <button class="btn btn-default" onclick="back()">返回</button>
           </div>
        </div>
    </div>
</body>

</html>

